package com.ybxx.asset.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.ybxx.asset.model.Building;
import com.ybxx.asset.model.Company;
import com.ybxx.asset.model.NightLife;
import com.ybxx.asset.model.Trench;
import com.ybxx.asset.model.vo.BuildingVO;
import com.ybxx.asset.model.vo.NightLifeVO;
import com.ybxx.asset.service.CompanyService;
import com.ybxx.asset.service.NightLifeService;
import com.ybxx.asset.service.TrenchService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.util.DateUtil;
import com.ybxx.core.util.ExportExcel;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;

public class NightLifeAction extends BaseAction {
	@Resource
	private CompanyService companyService;
	@Resource
	private NightLifeService nightLifeService;
	private List<Company> companys;
	private List<NightLife> nightLifes;
	private List<NightLifeVO> vos;
	private NightLife nightLife;
	private String msg="succ";
	/*打印*/
	public String print(){
		nightLifes=nightLifeService.exportExcel_Print(nightLife);
		vos=new ArrayList<NightLifeVO>();
		if(nightLifes!=null){
			int i=1;
			for(NightLife nightLife:nightLifes){
				NightLifeVO vo=new NightLifeVO();
				vo.setSeq(i+"");
				vo.setAddr(nightLife.getAddr());
				vo.setArea(nightLife.getArea());
				vo.setCname(nightLife.getCompany().getCname());
				vo.setContent(nightLife.getContent());
				vo.setNetbookvalue(nightLife.getNetbookvalue());
				vo.setNlname(nightLife.getNlname());
				vo.setSzdj(nightLife.getSzdj());
				vo.setTdzh(nightLife.getTdzh());
				vo.setYwzy(nightLife.getYwzy());
				vo.setZdh(nightLife.getZdh());
				vos.add(vo);
				i++;
			}
		}
		return "print";
	}
	/*导出*/
	public String exportExcel(){
		nightLifes=nightLifeService.exportExcel_Print(nightLife);
		vos=new ArrayList<NightLifeVO>();
		if(nightLifes!=null){
			int i=1;
			for(NightLife nightLife:nightLifes){
				NightLifeVO vo=new NightLifeVO();
				vo.setSeq(i+"");
				vo.setAddr(nightLife.getAddr());
				vo.setArea(nightLife.getArea());
				vo.setCname(nightLife.getCompany().getCname());
				vo.setContent(nightLife.getContent());
				vo.setNetbookvalue(nightLife.getNetbookvalue());
				vo.setNlname(nightLife.getNlname());
				vo.setSzdj(nightLife.getSzdj());
				vo.setTdzh(nightLife.getTdzh());
				vo.setYwzy(nightLife.getYwzy());
				vo.setZdh(nightLife.getZdh());
				vos.add(vo);
				i++;
			}
		}
		ExportExcel<NightLifeVO> ex=new ExportExcel<NightLifeVO>();
		OutputStream out;
		try {
			out = super.getResponse().getOutputStream();
			super.getResponse().reset();// 清空输出流
			super.getResponse().setContentType("text/html; charset=UTF-8");
			super.getResponse().setHeader("Content-disposition",
					"attachment; filename="+DateUtil.formatDateYYYYMMdd(new Date())+".xls");// 设定输出文件头
			super.getResponse().setContentType("application/msexcel");// 定义输出类型
			String[] headers={"序号","企业名称","土地证号","土地名称","地址","面积","宗地号","账面净值","四至地界","有无争议","备注"};
	        ex.exportExcel("房屋建筑物信息",headers, vos, out);
	        out.flush();
	        out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/*删除*/
	public String remove(){
		try{
			nightLifeService.remove(nightLife.getNlid());
		}catch(Exception ex){
			msg="生活区土地信息删除失败";
		}finally{
			return "succ";
		}
	}
	/*加载查看时候需要展示的数据*/
	public String view(){
		if(nightLife!=null && StringUtils.isNotEmpty(nightLife.getNlid())){
			nightLife=nightLifeService.get(nightLife.getNlid());
		}
		return "view";
	}
	public String init(){
		companys=companyService.getAll();
		return "init";
	}
	public String queryPage(){
		QueryFilter filter=new QueryFilter(getRequest());
		nightLifes=nightLifeService.queryPage(nightLife,filter);
		super.setRownum(filter.getPagingBean().getTotalItems());
		super.setCurrentpage(Integer.parseInt(filter.getPagingBean().getStart()+"")/Integer.parseInt(filter.getPagingBean().getPageSize()+"")+1);
		super.setPageSize(Integer.parseInt(filter.getPagingBean().getPageSize()+""));
		return "queryPage";
	}
	/*保存新增，修改信息*/
	public String save(){
		if(StringUtils.isEmpty(nightLife.getNlid())){
			nightLife.setNlid(UUIDGenerator.getUUID());
		}
		try{
			nightLifeService.save(nightLife);
		}catch(Exception ex){
			msg="生活区土地信息保存失败.";
		}finally{
			return "succ";
		}
	}
	/*加载修改时候需要展示的数据*/
	public String load(){
		companys=companyService.getAll();
		if(nightLife!=null && StringUtils.isNotEmpty(nightLife.getNlid())){
			nightLife=nightLifeService.get(nightLife.getNlid());
		}
		return "load";
	}
	public List<Company> getCompanys() {
		return companys;
	}
	public void setCompanys(List<Company> companys) {
		this.companys = companys;
	}
	public List<NightLife> getNightLifes() {
		return nightLifes;
	}
	public void setNightLifes(List<NightLife> nightLifes) {
		this.nightLifes = nightLifes;
	}
	public NightLife getNightLife() {
		return nightLife;
	}
	public void setNightLife(NightLife nightLife) {
		this.nightLife = nightLife;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public CompanyService getCompanyService() {
		return companyService;
	}
	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}
	public NightLifeService getNightLifeService() {
		return nightLifeService;
	}
	public void setNightLifeService(NightLifeService nightLifeService) {
		this.nightLifeService = nightLifeService;
	}
	public List<NightLifeVO> getVos() {
		return vos;
	}
	public void setVos(List<NightLifeVO> vos) {
		this.vos = vos;
	}
}
